package com.example.birdfriends.service.impl;

import com.example.birdfriends.dao.export.DataExportDao;
import com.example.birdfriends.dao.export.DataExportExcelDao;
import com.example.birdfriends.pojo.export.constant.DataExportTypeConstant;
import com.example.birdfriends.pojo.export.entity.DataExportEntity;
import com.example.birdfriends.pojo.export.entity.DataExportExcelEntity;
import com.example.birdfriends.pojo.export.vo.DataExportVo;
import com.example.birdfriends.service.DataExportWriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class DataExportWriteImpl implements DataExportWriteService {
    @Autowired
    private DataExportDao dataExportDao = null;
    @Autowired
    private DataExportExcelDao dataExportExcelDao = null;

    @Override
    public void updateDataExportStat(DataExportVo dataExportVo) {
        DataExportEntity dataExportEntity = new DataExportEntity();
        dataExportEntity.setExportId(dataExportVo.getExportId());
        dataExportEntity.setExportStat(dataExportVo.getExportStat());
        dataExportDao.updateDataExportStat(dataExportEntity);
    }

    @Override
    public void addDataExport(DataExportVo dataExportVo) {
        DataExportEntity dataExportEntity = new DataExportEntity();
        dataExportEntity.setExportId(UUID.randomUUID().toString().replace("-", ""));
        dataExportEntity.setExportName(dataExportVo.getExportName());
        dataExportEntity.setSql("select * from "+dataExportVo.getExportTable());
        dataExportEntity.setExportTable(dataExportVo.getExportTable());
        dataExportEntity.setExportTime(new Date());
        dataExportEntity.setExportStat(DataExportTypeConstant.RUN_EXPORT.getValue());
        dataExportDao.addDataExport(dataExportEntity);

        DataExportExcelEntity dataExportExcelEntity = new DataExportExcelEntity();
        dataExportExcelEntity.setExportId(dataExportEntity.getExportId());
        dataExportExcelEntity.setExcelId(UUID.randomUUID().toString().replace("-", ""));
        dataExportExcelDao.addDataExportExcel(dataExportExcelEntity);
    }
}
